# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

# class Solution:
#     def reverseList(self, head: ListNode) -> ListNode:
#         if not head or not head.next:
#             return head
#         tail=head.next
#         head.next=None
#         while tail.next:
#             temp=tail.next
#             tail.next=head
#             head=tail
#             tail=temp
#         tail.next=head
#         return tail

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        tail=head
        head=None
        while tail:
            temp=tail.next
            tail.next=head
            head=tail
            tail=temp
        return head